package org.vartist.atld.menu.activity.common;

import java.util.ArrayList;

import org.vartist.atld.R;
import org.vartist.atld.menu.activity.main.MainActivity;
import org.vartist.atld.menu.activity.util.UtilActivity;
import org.vartist.atld.menu.activity.util.base64.Base64Activity;
import org.vartist.atld.menu.activity.view.ViewActivity;
import org.vartist.atld.menu.adapter.MenuListAdapter;
import org.vartist.atld.menu.consts.LayoutConst;
import org.vartist.atld.menu.generator.MenuGenerator;
import org.vartist.atld.menu.listener.main.MenuOnItemClickListener;
import org.vartist.atld.menu.vo.MenuVO;
import org.vartist.atld.util.log.LogUtil;
import org.vartist.atld.util.ui.TitleUtil;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public abstract class CommonMenuList extends Activity {
	
	protected ListView mLstMenu = null;
	protected ArrayList<MenuVO> mMenuList = null;
	
	protected CommonBackPressed mCommonBackPressed = null; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mCommonBackPressed = new CommonBackPressed(this);
		
        TitleUtil.setContentView(this, LayoutConst.LAYOUT_FOR_LIST, getString(R.string.app_name), getCategoryName(this));
        getLayouts();
        
        mMenuList = generateMenuList();
        setMenuListAdapter();
        
        MenuOnItemClickListener itemClickListener = generateMenuItemClickListener();
        setMenuItemClickListener(itemClickListener);
	}

	@Override
	public void onBackPressed() {
		mCommonBackPressed.onBackPressed();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	private void setMenuListAdapter() {
		if (mMenuList != null) {
			if (mMenuList.size() > 0) {
				if (mLstMenu != null) {
					mLstMenu.setAdapter(new MenuListAdapter(this, LayoutConst.LAYOUT_FOR_LIST_ROW, mMenuList));	
				} else {
					LogUtil.e(mLstMenu.getClass().getName() + " " + getString(R.string.error_msg_null));
				}
			} else {
				LogUtil.e(mMenuList.getClass().getName() + " " + getString(R.string.error_msg_size_zero));
			}
		} else {
			LogUtil.e(mMenuList.getClass().getName() + " " + getString(R.string.error_msg_null));
		}
		
	}

	private ArrayList<MenuVO> generateMenuList() {
		return MenuGenerator.makeMenuList(this);
	}

	private MenuOnItemClickListener generateMenuItemClickListener() {
		if (mMenuList != null) {
			return new MenuOnItemClickListener(this, mMenuList);	
		} else {
			LogUtil.e(mMenuList.getClass().getName() + " " + getString(R.string.error_msg_null));
			return null;
		}
		
	}

	private void setMenuItemClickListener(MenuOnItemClickListener itemClickListener) {
		if (itemClickListener != null) {
			if (mLstMenu != null) {
				mLstMenu.setOnItemClickListener(itemClickListener);
			} else {
				LogUtil.e(mMenuList.getClass().getName() + " " + getString(R.string.error_msg_null));
			}
		}
				
	}

	private String getCategoryName(Activity activity) {
		String categoryName = null;
		
		if (activity != null) {
			
			/***
			 * Main
			 */
			if (activity instanceof MainActivity) {
				categoryName = getString(R.string.menu_main_name);
			} 
			
			/***
			 * Util 
			 */
			else if (activity instanceof UtilActivity) {
				categoryName = getString(R.string.menu_util_name);
			} 
			
			else if (activity instanceof Base64Activity) {
				categoryName = getString(R.string.menu_util_base64_name);
			}
			
			/***
			 * View
			 */
			else if (activity instanceof ViewActivity) {
				categoryName = getString(R.string.menu_view_name);
			} 
			
			/***
			 * Other
			 */
			else {
				categoryName = getString(R.string.category_no_exist_info);
			}
		}
		
		return categoryName;
	}

	public void getLayouts() {
		mLstMenu = (ListView) findViewById(R.id.mainListView);
	}
	
}
